/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package adsreis.com.Controls;

import adsreis.com.Beans.OSdetBean;
import adsreis.com.Beans.OrcamentosdetBean;
import adsreis.com.acessobd.AcessoMySql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Pauroenriki
 */
public class OrcamentosdetControl {
    
    PreparedStatement pstm;
    ResultSet rs;
    String cadastraItem = "INSERT INTO ORCAMENTOSDET(IDORCAMENTO,IDPRODUTO, QTDORCAMENTO, VLRVENDA) VALUES(?,?,?,?)";
    String buscaUltimoCodigo = "SELECT MAX(CODORCAMENTO) AS CODORCAMENTO FROM ORCAMENTOS";
    String deleta = "DELETE FROM ORCAMENTODET WHERE IDORCAMENTO = ?";
    
    
    
    
    
    
    
    
    
    public OrcamentosdetControl(){
        
    }
    
    
    public void excluirOrcamentoDET(int idorcamento){
        try{
            AcessoMySql mysql = new AcessoMySql();
           pstm = mysql.conectar().prepareStatement(deleta); 
           pstm.setInt(1, idorcamento);
           pstm.executeUpdate();
           mysql.desconectar();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
    
    public int buscarCodigoUltimOrcamento(){
        try{
            AcessoMySql mysql = new AcessoMySql();
            pstm = mysql.conectar().prepareStatement(buscaUltimoCodigo);
            rs = pstm.executeQuery();
            if (rs.last()){
                return rs.getInt("codorcamento");                
            }
            mysql.desconectar();
        }catch (SQLException ex){
            ex.printStackTrace();
        }
        return 0;
    }
    
    public void cadastrarItem(OrcamentosdetBean orcamentodet){
        try{
            AcessoMySql mysql = new AcessoMySql();
            pstm = mysql.conectar().prepareStatement(cadastraItem);
            pstm.setInt(1, orcamentodet.getIDOrcamento());
            pstm.setString(2, orcamentodet.getIDProduto());
            pstm.setInt(3, orcamentodet.getQtdOrcamento());
            pstm.setDouble(4, orcamentodet.getPreco());
            pstm.executeUpdate();
            mysql.desconectar();
        }catch(SQLException ex){
            ex.printStackTrace();
        }
    }
    
    
   
    
    
}
